// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Push Block Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 68 : TYPE_PUSHBLOCK

// Function declarations
reserve function PushBlock_DebugDraw
reserve function PushBlock_DebugSpawn
reserve function PushBlock_Function156
reserve function PushBlock_Function157
reserve function PushBlock_Function158

// Static Values
public value PushBlock_value37 = 0;

// Tables

function PushBlock_DebugDraw
	temp0 = object[0].value17
	temp0 -= PushBlock_value37
	DrawSprite(temp0)
end function


function PushBlock_DebugSpawn
	CreateTempObject(TypeName[Push Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].value6 = 0
	object[tempObjectPos].frame = object[0].value17
	object[tempObjectPos].frame -= PushBlock_value37
	if object[tempObjectPos].frame == 0
		object[tempObjectPos].value2 = -16
		object[tempObjectPos].value3 = 16
		object[tempObjectPos].value4 = -17
		object[tempObjectPos].value5 = 17
	else
		object[tempObjectPos].value2 = -64
		object[tempObjectPos].value3 = 64
		object[tempObjectPos].value4 = -65
		object[tempObjectPos].value5 = 65
		object[tempObjectPos].drawOrder = 5
	end if
end function


function PushBlock_Function156
	if object.floorSensorC == 0
		if object.floorSensorL == 0
			object.xvel = -0x40000
			object.value9 = object.xpos
			object.value9 &= -0x100000
			object.state++
		else
			if object.floorSensorR == 0
				object.xvel = 0x40000
				object.value9 = object.xpos
				object.value9 &= -0x100000
				object.value9 += 0x100000
				object.state++
			end if
		end if
	end if
end function


function PushBlock_Function157
	object.value10 = 0
	temp2 = 0
	object.xvel = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(options.attractMode, 1)
		temp1 = checkResult
		CheckEqual(currentPlayer, 1)
		temp1 &= checkResult
		if object[currentPlayer].gravity == GRAVITY_GROUND
			if object[currentPlayer].xvel < 0
				BoxCollisionTest(C_TOUCH, object.entityPos, 0, -12, object.value5, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					if object[currentPlayer].left == 1
						if temp1 == 0
							SetBit(object.value10, temp2, 1)
							object.xvel -= 0x6000
							object.value6 = 1
						end if
					end if
					object[currentPlayer].xpos = object[currentPlayer].collisionRight
					object[currentPlayer].xpos += object.value3
					object[currentPlayer].xpos <<= 16
					object[currentPlayer].xpos += object.value1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].pushing = 2
				end if
			else
				if object[currentPlayer].xvel > 0
					BoxCollisionTest(C_TOUCH, object.entityPos, object.value4, -12, 0, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						if object[currentPlayer].right == 1
							if temp1 == 0
								SetBit(object.value10, temp2, 1)
								object.xvel += 0x6000
								object.value6 = 1
							end if
						end if
						object[currentPlayer].xpos = object[currentPlayer].collisionLeft
						object[currentPlayer].xpos += object.value2
						object[currentPlayer].xpos <<= 16
						object[currentPlayer].xpos += object.value1
						object[currentPlayer].xvel = 0
						object[currentPlayer].speed = 0
						object[currentPlayer].pushing = 2
					end if
				else
					BoxCollisionTest(C_BOX, object.entityPos, object.value2, -16, object.value3, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				end if
			end if
		else
			BoxCollisionTest(C_BOX, object.entityPos, object.value2, -16, object.value3, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		end if
		temp2++
	next
	if object.xvel != 0
		object.xpos += object.xvel
		if object.xvel < 0
			ObjectTileCollision(CSIDE_RWALL, object.value2, 0, 0)
		else
			ObjectTileCollision(CSIDE_LWALL, object.value3, 0, 0)
		end if
		CallFunction(PushBlock_Function156)
		temp0 = object.xpos
		temp0 &= -0x10000
		if object.value1 != temp0
			temp0 = oscillation
			temp0 &= 31
			if temp0 < 3
				PlaySfx(SfxName[Push Block], 0)
			end if
		end if
	end if
	object.value1 = object.xpos
	object.value1 &= -0x10000
	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.value10, temp2)
		if temp0 == 1
			if object[currentPlayer].xpos < object.xpos
				object[currentPlayer].ixpos = object[currentPlayer].collisionRight
				temp1 = object.value2
			else
				object[currentPlayer].ixpos = object[currentPlayer].collisionLeft
				temp1 = object.value3
			end if
			FlipSign(object[currentPlayer].xpos)
			temp1 <<= 16
			object[currentPlayer].xpos += temp1
			object[currentPlayer].xpos += object.xpos
		else
			BoxCollisionTest(C_BOX, object.entityPos, object.value2, -16, object.value3, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		end if
		temp2++
	next
end function


function PushBlock_Function158
	if object.value6 == 1
		if object.gravity == GRAVITY_AIR
			object.yvel += 0x1800
			object.ypos += object.yvel
			temp0 = object.value2
			temp0 += 2
			ObjectTileCollision(CSIDE_FLOOR, temp0, 16, 0)
			object.floorSensorL = checkResult
			object.gravity = checkResult
			ObjectTileCollision(CSIDE_FLOOR, 0, 16, 0)
			object.floorSensorC = checkResult
			object.gravity |= checkResult
			temp0 = object.value3
			temp0 -= 2
			ObjectTileCollision(CSIDE_FLOOR, temp0, 16, 0)
			object.floorSensorR = checkResult
			object.gravity |= checkResult
		else
			object.yvel = 0
			temp0 = object.value2
			temp0 += 2
			ObjectTileGrip(CSIDE_FLOOR, temp0, 16, 0)
			object.floorSensorL = checkResult
			object.gravity = checkResult
			ObjectTileGrip(CSIDE_FLOOR, 0, 16, 0)
			object.floorSensorC = checkResult
			object.gravity |= checkResult
			temp0 = object.value3
			temp0 -= 2
			ObjectTileGrip(CSIDE_FLOOR, temp0, 16, 0)
			object.floorSensorR = checkResult
			object.gravity |= checkResult
		end if
		object.gravity ^= GRAVITY_AIR
	else
		object.floorSensorL = 1
		object.floorSensorC = 1
		object.floorSensorR = 1
	end if
end function


event ObjectMain
	temp6 = object.xpos
	temp7 = object.ypos
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0			
			CallFunction(PushBlock_Function157)
		end if	
		CallFunction(PushBlock_Function158)
		break
	case 1
		object.xpos += object.xvel
		if object.xvel > 0
			if object.xpos >= object.value9
				object.xpos = object.value9
				object.xvel = 0
				object.gravity = GRAVITY_AIR
				object.state = 2
			end if
		else
			if object.xpos <= object.value9
				object.xpos = object.value9
				object.xvel = 0
				object.gravity = GRAVITY_AIR
				object.state = 2
			end if
		end if
		break
	case 2
		CallFunction(PushBlock_Function158)
		if object.gravity == GRAVITY_GROUND
			object.state = 0
		end if
		break
	end switch
	temp0 = object.xpos
	temp1 = object.ypos
	temp4 = object.xpos
	temp4 &= -0x10000
	temp5 = object.ypos
	temp5 &= -0x10000
	object.xpos = temp6
	object.ypos = temp7
	temp6 &= -0x10000
	temp7 &= -0x10000
	temp4 -= temp6
	temp5 -= temp7
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0		
			CheckNotEqual(object[currentPlayer].pushing, 0)
			temp2 = checkResult
			CheckEqual(object.state, 0)
			temp2 &= checkResult
			if temp2 == 0
				BoxCollisionTest(C_BOX, object.entityPos, object.value2, -16, object.value3, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object[currentPlayer].xpos += temp4
					object[currentPlayer].ypos += temp5
				end if
			end if
		else
			object[currentPlayer].pushing = 2	
		end if		
	next
	object.xpos = temp0
	object.ypos = temp1
	object.value1 = object.xpos
	object.value1 &= -0x10000
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value7
		object.ypos = object.value8
		if object.outOfBounds == 1
			object.yvel = 0
			object.value6 = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
end event


event ObjectDraw
	DrawSpriteXY(object.frame, object.value1, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 84, 223)
	SpriteFrame(-64, -16, 128, 32, 84, 223)
	foreach (TypeName[Push Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].frame = object[arrayPos0].propertyValue
		object[arrayPos0].frame &= 1
		object[arrayPos0].value7 = object[arrayPos0].xpos
		object[arrayPos0].value8 = object[arrayPos0].ypos
		object[arrayPos0].value6 = 0
		if object[arrayPos0].frame == 0
			object[arrayPos0].value2 = -16
			object[arrayPos0].value3 = 16
			object[arrayPos0].value4 = -17
			object[arrayPos0].value5 = 17
		else
			object[arrayPos0].value2 = -64
			object[arrayPos0].value3 = 64
			object[arrayPos0].value4 = -65
			object[arrayPos0].value5 = 65
		end if
	next
	temp0 = 0
	PushBlock_value37 = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Push Block], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(PushBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(PushBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
